package com.base.api.pms.impl;

import com.alibaba.fastjson.JSONObject;
import com.base.api.pms.PmsFollowItemService;
import com.base.dao.pms.PmsFollowItemMapper;
import com.base.vo.pms.PmsFollowItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wnhuang
 * @Package com.base.api.pms.impl
 * @date 2021-1-3 16:41
 */
@Service
public class PmsFollowItemServiceImpl implements PmsFollowItemService {

    @Autowired
    PmsFollowItemMapper pmsFollowItemMapper;

    @Override
    public String getFollowItem(Map<String, Object> params) {
        Map<String, Object> map = new HashMap<>();
        try {
            String itemClass = (String) params.get("itemClass");
            map.put("Data", getFollowItemByItemClass(itemClass));
            map.put("code", "0");
        } catch (Exception e) {
            map.put("code", "error");
            map.put("msg", e.getMessage());
        }
        return JSONObject.toJSONString(map);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String saveFollowItem(Map<String, Object> params) {
        Map<String, Object> map = new HashMap<>();
        String itemClass = (String) params.get("itemClass");
        pmsFollowItemMapper.delPmsFollowTestItem(itemClass);
        saveFollowItemByItemClass(itemClass, (List<String>) params.get("data"));
        map.put("code", "0");
        return JSONObject.toJSONString(map);
    }

    private List<PmsFollowItem> getFollowItemByItemClass(String itemClass) {
        if ("C".equals(itemClass)) {
            return pmsFollowItemMapper.getPmsFollowTestItem();
        }
        return null;
    }

    private void saveFollowItemByItemClass(String itemClass, List<String> list) {
        if ("C".equals(itemClass)) {
            pmsFollowItemMapper.savePmsFollowTestItem(list);
        }
    }
}
